package com.gzxx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gzxx.constant.ServiceCodeStatus;
import com.gzxx.domain.base.BaseDto;
import com.gzxx.domain.base.BaseVo;
import com.gzxx.domain.converter.SysRoleConverter;
import com.gzxx.domain.dto.SysRoleCreateDto;
import com.gzxx.domain.dto.SysRoleQueryDto;
import com.gzxx.domain.dto.SysRoleUpdateDto;
import com.gzxx.domain.vo.SysRoleVo;
import com.gzxx.service.SysRoleService;
import com.gzxx.util.ServerResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author MAX
 * @date 2020/7/27 8:58 上午
 * 系统角色路由
 */

@Api(tags = "系统角色相关接口")
@RestController
@RequestMapping("roles")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysRoleController {

    private final SysRoleService sysRoleService;

    /**
     * 列表
     */
    @ApiOperation("分页查询角色信息")
    @PostMapping("/list")
    @PreAuthorize("@rbac.hasPermission('system:roles:list')")
    public ServerResponse<SysRoleVo> list(@RequestBody SysRoleQueryDto sysRoleQueryDto) {
        if (ObjectUtils.isEmpty(sysRoleQueryDto)) {
            sysRoleQueryDto = new SysRoleQueryDto();
        }
        IPage<SysRoleVo> page = sysRoleService.getRoles(SysRoleConverter.MAPPER.convertQueryDto(sysRoleQueryDto));
        return ServerResponse.success(page);
    }

    /**
     * 详情
     */
    @ApiOperation("根据角色id获取角色明细")
    @GetMapping("/detail")
    @PreAuthorize("@rbac.hasPermission('system:roles:select')")
    public ServerResponse<SysRoleVo> detail(@RequestParam("id") Long roleId) {
        Assert.state(roleId > 0, "角色id必须大于0");
        SysRoleVo sysRoleVo = sysRoleService.getDetail(roleId);
        return ServerResponse.success(sysRoleVo);
    }

    /**
     * 新增
     */
    @ApiOperation("新增角色信息")
    @PostMapping("/add")
    @PreAuthorize("@rbac.hasPermission('system:roles:add')")
    public ServerResponse add(@Valid @RequestBody SysRoleCreateDto sysRoleCreateDto) {
        boolean result = sysRoleService.add(SysRoleConverter.MAPPER.convertCreateDto(sysRoleCreateDto));
        return result ? ServerResponse.success(ServiceCodeStatus.SUCCESS) : ServerResponse.error();
    }

    /**
     * 更新
     */
    @ApiOperation("更新角色信息")
    @PostMapping("/update")
    @PreAuthorize("@rbac.hasPermission('system:roles:update')")
    public ServerResponse update(@RequestBody SysRoleUpdateDto sysRoleUpdateDto) {
        return sysRoleService.update(SysRoleConverter.MAPPER.convertUpdateDto(sysRoleUpdateDto));
    }

    /**
     * 变更角色状态
     */
    @ApiOperation("变更角色状态")
    @PostMapping("/status")
    @PreAuthorize("@rbac.hasPermission('system:roles:update:status')")
    public ServerResponse status(@Valid @RequestBody BaseDto baseDto) {
        return sysRoleService.changeStatus(SysRoleConverter.MAPPER.convertBaseDto(baseDto));
    }

    /**
     * 获取有效角色，不分页
     */
    @ApiOperation("获取有效角色信息")
    @GetMapping("/types")
    public ServerResponse<BaseVo> types() {
        List<BaseVo> list = sysRoleService.getAll();
        return ServerResponse.success(list);
    }


    /**
     * 获取角色和关联用户 树形
     */
    @ApiOperation("获取角色和关联用户 树形")
    @GetMapping("/tree")
    public ServerResponse<BaseVo> tree() {
        List<BaseVo> list = sysRoleService.tree();
        return ServerResponse.success(list);
    }
}
